package io.dcloud.h527baaa6.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.dcloud.h527baaa6.R;
import io.dcloud.h527baaa6.utils.StatusBarUtil;
import io.dcloud.h527baaa6.view.MyLoadingView;
import io.dcloud.h527baaa6.view.ToolBarTitleView;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


public abstract class BaseActivity extends AppCompatActivity {

    protected ToolBarTitleView mToobarTitleView;
    protected MyLoadingView mLoadingView;

    protected Activity mContext;
    protected Unbinder mUnbinder;
    protected CompositeDisposable mCompositeDisposable;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        mToobarTitleView = (ToolBarTitleView) findViewById(R.id.toobar_title_view);
        initToolBar();
        initViewAndData();
        if(!isFullScreen())
            StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.base_color));
    }


    /**
     * 初始化ToolBar 可重写修改属性
     * 传0 代表没有标题栏
     */
    protected void initToolBar() {

        if (getTitleText() != 0)
            mToobarTitleView.setTitleText(getString(getTitleText()));
        else
            mToobarTitleView.setVisibility(View.GONE);

        mToobarTitleView.setTitleBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void onResume() {
        super.onResume();
    }
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null)
            mUnbinder.unbind();
        dispose();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(R.layout.activity_title_base_layout);
        FrameLayout baseLayout = (FrameLayout) findViewById(R.id.base_layout);
        mLoadingView = (MyLoadingView) findViewById(R.id.base_loading);
        View realLayout = LayoutInflater.from(this).inflate(layoutResID,baseLayout,false);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        baseLayout.addView(realLayout,0,params);
    }

    /**
     * 添加订阅到集合
     */
    protected void addDisposable(Disposable disposable) {

        if (mCompositeDisposable == null)
            mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(disposable);
    }

    /**
     * 解除所有订阅
     */
    protected void dispose() {
        if (mCompositeDisposable != null)
            mCompositeDisposable.dispose();
        mCompositeDisposable = null;
    }

    protected boolean isFullScreen()
    {
        return false;
    }

    /**
     * 获取布局Id
     */
    protected abstract @LayoutRes
    int getLayout();

    /**
     * 获取标题title
     */
    protected abstract @StringRes
    int getTitleText();

    /**
     * 初始化布局和数据
     */
    protected abstract void initViewAndData();
}
